// Copyright � 2002-2010 Microchip Technology Inc.  All rights reserved.
// See Microchip TCP/IP Stack documentation for license information.


// Determines when a request is considered "timed out"
var timeOutMS = 5000; //ms

// Stores a queue of AJAX events to process
var ajaxList = new Array();
var StopAJAX = 0;

function KillAjax()
{
    StopAJAX = 1;
}
// Initiates a new AJAX command
//	url: the url to access
//	container: the document ID to fill, or a function to call with response XML (optional)
//	repeat: true to repeat this call indefinitely (optional)
//	data: an URL encoded string to be submitted as POST data (optional)
function newAJAXCommand(url, method , container, repeat, data)
{
    // Set up our object
    var newAjax = new Object();
    var theTimer = new Date();
    newAjax.url = url;
    newAjax.method = method;
    newAjax.container = container;
    newAjax.repeat = repeat;
    newAjax.ajaxReq = null;

    if (StopAJAX)
    {
        //StopAJAX = 0;
        return;
    }

    // Create and send the request
    if (window.XMLHttpRequest) {
        newAjax.ajaxReq = new XMLHttpRequest();
        newAjax.ajaxReq.open(method, newAjax.url, true);
        //This next line send Basic Authentication for root:root
        //newAjax.ajaxReq.setRequestHeader("Authorization","Basic cm9vdDpyb290" );
        newAjax.ajaxReq.send(data);
        // If we're using IE6 style (maybe 5.5 compatible too)
    } else if (window.ActiveXObject) {
        newAjax.ajaxReq = new ActiveXObject("Microsoft.XMLHTTP");
        if (newAjax.ajaxReq) {
            newAjax.ajaxReq.open(method, newAjax.url, true);
            newAjax.ajaxReq.send(data);
        }
    }

    newAjax.lastCalled = theTimer.getTime();

    // Store in our array
    ajaxList.push(newAjax);
}

// Loops over all pending AJAX events to determine if any action is required
function pollAJAX() {
    var curAjax = new Object();
    var theTimer = new Date();
    var elapsed;

    // Read off the ajaxList objects one by one
    for (i = ajaxList.length; i > 0; i--)
    {
        curAjax = ajaxList.shift();
        if (!curAjax)
            continue;
        elapsed = theTimer.getTime() - curAjax.lastCalled;

        // If we suceeded
        if (curAjax.ajaxReq.readyState == 4 && curAjax.ajaxReq.status == 200) {
            // If it has a container, write the result
            if (typeof(curAjax.container) == 'function') {
                //document.write(curAjax.ajaxReq.responseText);
                curAjax.container(curAjax.ajaxReq.responseXML.documentElement);
//                            if (curAjax.container(curAjax.ajaxReq.responseXML.documentElement))
//                                {
//                                    curAjax.container(curAjax.ajaxReq.responseXML.documentElement);
//                                }else
//                                {
//                                    //curAjax.container(curAjax.ajaxReq.responseText);
//                                }
            } else if (typeof(curAjax.container) == 'string') {
                document.getElementById(curAjax.container).innerHTML = curAjax.ajaxReq.responseText;
            } // (otherwise do nothing for null values)

            curAjax.ajaxReq.abort();
            curAjax.ajaxReq = null;

            // If it's a repeatable request, then do so
            if (curAjax.repeat)
                newAJAXCommand(curAjax.url, curAjax.method, curAjax.container, curAjax.repeat);
            continue;
        }

        // If we've waited over 1 second, then we timed out
        if (elapsed > timeOutMS) {
            // Invoke the user function with null input
            if (typeof(curAjax.container) == 'function') {
                curAjax.container(null);
            } else {
                // Alert the user
                alert("Command failed.\nConnection to development board was lost.");
            }

            curAjax.ajaxReq.abort();
            curAjax.ajaxReq = null;

            // If it's a repeatable request, then do so
            if (curAjax.repeat)
                newAJAXCommand(curAjax.url, curAjax.method, curAjax.container, curAjax.repeat);
            continue;
        }

        // Otherwise, just keep waiting
        ajaxList.push(curAjax);
    }

    // Call ourselves again in 200ms
    setTimeout("pollAJAX()", 200);

}

// Parses the xmlResponse returned by an XMLHTTPRequest object
//	xmlData: the xmlData returned
//  field: the field to search for
function getXMLValue(xmlData, field) {
    try {
        if (xmlData.getElementsByTagName(field)[0].firstChild.nodeValue)
            return xmlData.getElementsByTagName(field)[0].firstChild.nodeValue;
        else
            return null;
    } catch (err) {
        return null;
    }
}

//kick off the AJAX Updater
setTimeout("pollAJAX()", 500);

